'use strict';

angular.module('ProduceReady').controller('MOListController', ['$scope', 'ExtendEnumService', 'BaseModule', 'ProduceReadyEnum', 'BaseEnum', 'BaseFunction', '$location', 'Authentication',
    function($scope, ExtendEnumService, BaseModule, ProduceReadyEnum, BaseEnum, BaseFunction, $location, Authentication) {
        ExtendEnumService.getExtendEnum('ItemCategory', function(extendEnum){
            $scope.ItemCategoryEnum = extendEnum;
        });
        $scope.MOStatusEnum = BaseFunction.getEnum('ProduceReadyEnum.MOStatus');

    	BaseModule.initModule($scope, 'MO');
        $scope.Module.MO.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
        $scope.Module.MO.Populate = [{path: 'ItemMaster', select: 'Code Name'}];
        $scope.Module.MO.PageSize = 15;

        //将Qty大于等于100的按ItemMaster分组并返回
        //返回[ItemMaster._id]
        // $scope.Module.MO.distinct('ItemMaster', {Qty: {$gte: 100}}).then(function(data){
        //     console.log(data);
        // });

        //将Qty大于等于100的按ItemMaster分组，返回最大值并按值升序排序
        //返回[{_id: ItemMaster._id, value: 该组最大值}]
        // $scope.Module.MO.aggregate([
        //         {$match: {Qty: {$gte: 100}}},
        //         {$group : {_id : '$' + 'ItemMaster', value : {$max: '$' + 'Qty'}}},
        //         {$sort: {value: 1}}
        //     ]).then(function(data){
        //         console.log(data);
        // });

        //查询数量大于等于100的，按料品分组，求条数、求和、求平均，并且过滤掉结果里面条数大于等于10的，并按求和值排序
        //返回[{_id: ItemMaster._id, value: {Count: , Sum: , Avg: }}]
        // var map = function(){
        //     emit(this.ItemMaster, this.Qty);
        // };
        // var reduce = function(key, values){
        //     var total = 0;
        //     for ( var i=0; i<values.length; i++ ){
        //         total += values[i];
        //     }
            
        //     return {
        //         Count: values.length,
        //         Sum: total,
        //         Avg: total / values.length
        //     };
        // };
        // var option = {};
        // option.map = map.toString();
        // option.reduce = reduce.toString();
        // option.query = {Qty: {$gte: 100}};
        // var condition = {'value.Count': {$gte: 10}};     //结果集过滤条件
        // var orderBy = {'value.Sum': 1};                  //结果集排序条件
        // $scope.Module.MO.mapReduce(option, condition, orderBy).then(function(data){
        //         console.log(data);
        // });

        $scope.btnAdd = function(){
        	$location.url('/index/MO/Edit');
        };

        $scope.rowDblClick = function(){
            $scope.$apply(function(){
                $location.url("/index/MO/Edit/" + $scope.Module.MO.FocusedRecord._id);
            });
        };

        $scope.btnDelete = function(){
        	$scope.Module.MO.deleteSelecteds();
        };

        $scope.btnDownline = function(){
            var records = $scope.Module.MO.SelectedRecords;
            if(records.length == 0){
                BaseFunction.alert("没有选择下线记录", "下线提示");
                return;
            }
            var ids = [];
            for(var i=0; i<records.length; i++){
                if(records[i].Status == ProduceReadyEnum.MOStatus.Online.Value){
                    ids.push(records[i]._id);
                }
                else{
                    BaseFunction.alert("生产订单：" + records[i].Code + "不是开工状态，不能下线", "下线失败");
                    return;
                }
            }

            $scope.Module.MO.updateByValue({_id: {$in: ids}}, {Status: ProduceReadyEnum.MOStatus.Downline.Value}, function(){
                $scope.Module.MO.load();
            });
        };
    }
]);